package com.diyphonegadgets.videoreceiver;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;

public class downStreamOld extends Activity
{
	ImageView iv;
	Bitmap bmp;
	int p = 8123;
	InputStream receivedData;
	final String TAG = "downStream [DownStream]";
	InputStream in = null;
	byte[] imgData = new byte[40054];
    DataInputStream dis =null;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		
		iv=(ImageView)findViewById(R.id.lpimg);
		
		
	
	
		
		DownStreamer ds=new DownStreamer(p);
		//ds.iv=iv;
		new Thread(ds).start();
		//ds.run();
	}
    
    
	final Handler messageHandler = new Handler() 
	{

		@Override
		public void handleMessage(Message msg) 
		{  
			iv.setImageBitmap(bmp);
        }
	};

    

	class DownStreamer implements Runnable
	{
		ServerSocket serverSocket;
		//public ImageView iv;
		boolean exception=false;
		
		public DownStreamer(int port)
		{
			p=port;
		}
		
		@Override
		public void run()
		{
			
			
			  boolean done = false;
		         while (!done)
		         {
			 ServerSocket server = null;
		      try 
		      {
		         server = new ServerSocket(p);
		      } 
		      catch (IOException e) 
		      {
		         System.out.println("Error on port:  " +p+ ", " + e);
		      }

		      System.out.println("Server setup and waiting for client connection ...");

		      Socket client=null;
		      try 
		      {
		        client  = server.accept();
		      } 
		      catch (IOException e) 
		      {
		         System.out.println("Did not accept connection: " + e);
		      }

		      System.out.println("Client connection accepted. Moving to local port ...");
		      
		    

		      try
		      {
		    	  
		    	 
		         InputStream streamIn = client.getInputStream();

		        
		            
		              makeBitmapImage(streamIn);
		               
		             
		         

		         streamIn.close();
		         client.close();
		         server.close();
		         
		         
		         try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
		         
			         }
		      
		      catch(IOException e)
		      { System.out.println("IO Error in streams " + e); }
		   }
		         
		}
		      
		
				
	}	// end of downstreamer class
		
	
    
    
    
	  private void makeBitmapImage(final InputStream input){
		  
		  
		  new Thread(new Runnable() {
              @Override
              public void run() {
            	  Log.i(TAG,"tranforming into bitmap..."); 

      			
  				bmp=BitmapFactory.decodeStream(input);
  				if(bmp != null)
  				{
  					Log.i(TAG,"bitmap exists"); 

  					messageHandler.sendEmptyMessage(0);
  					Log.i(TAG,"Done.");
  					
  				
  					
  				}
  	  
  	        else{
  	        	
  				Log.i(TAG,"bitmap is null"); 

  	        }              }
          }).start();
	    	
		  
			
				
	  }
			
    
}	